#include "Fish.h"
#include "ShootEmUp.h"
#include "MainCharacter.h"
#include "GameManager.h"


using namespace Ogre;
using namespace std;

int Fish::ID=0;
const char* Fish::DEFAULT_NAME="fish_";
const char* Fish::DEFAULT_NODE_NAME="FishNode";

Fish::Fish()
:Item(Fish::DEFAULT_NAME + StringConverter::toString(Fish::ID))
{
	Fish::ID++;
	mSceneMgr = ShootEmUp::getSingleton().getSceneManager();
	initialize();
}

Fish::~Fish()
{}

void Fish::initialize()
{
	moveSpeed = ShootEmUp::getSingleton().getScrollingSpeed();

	mEntity = mSceneMgr->createEntity(getName(), "fish.mesh");
	mEntity->setUserObject( this );
	

	//mSceneNode = static_cast<SceneNode*>(mSceneMgr->getSceneNode(Fish::DEFAULT_NODE_NAME)->createChild());//static_cast<SceneNode*>(mSceneMgr->getSceneNode("ScrollingNode")->createChild());
	mSceneNode = mSceneMgr->getSceneNode(Fish::DEFAULT_NODE_NAME)->createChildSceneNode(Fish::DEFAULT_NODE_NAME+string("_")+getName());
	Vector3 initialPosition( 
		Math::RangeRandom(ShootEmUp::WORLD_MIN.x, ShootEmUp::WORLD_MAX.x), 
		Math::RangeRandom(ShootEmUp::WORLD_MIN.y, ShootEmUp::WORLD_MAX.y), 
		ShootEmUp::WORLD_MAX.z
		);
	mSceneNode->setPosition( initialPosition );
	mSceneNode->scale(8., 8., 8.);
	mSceneNode->attachObject( mEntity );
}

void Fish::collision(Item* _item)
{
	_item->collisionWithFish(this);
}

void Fish::collisionWithBullet(Bullet* _bullet)
{}

void Fish::collisionWithEnemy(Enemy* _enemy)
{}

void Fish::collisionWithMainCharacter(MainCharacter* _mainCharacter)
{
	_mainCharacter->fishWon();
	ShootEmUp::getSingleton().showFish( true );
	GameManager::getSingleton().destroyFish( this );
}

void Fish::collisionWithBonus(Bonus* _bonus)
{}

void Fish::collisionWithWeapon(Weapon* weapon)
{}

bool Fish::frameStarted(const FrameEvent& evt)
{
	move();
	return true;
}


void Fish::collisionWithFish(Fish * _fish){
	// do nothing !
}


bool Fish::move(void)
{
	if ( mSceneNode->getPosition().z >= (ShootEmUp::WORLD_MIN.z - 100) )
		mSceneNode->translate(Vector3::NEGATIVE_UNIT_Z * moveSpeed);
	else
		GameManager::getSingleton().destroyFish( this );
	return true;
}

void Fish::reset()
{
	Vector3 initialPosition( 
		Math::RangeRandom(ShootEmUp::WORLD_MIN.x, ShootEmUp::WORLD_MAX.x), 
		Math::RangeRandom(ShootEmUp::WORLD_MIN.y, ShootEmUp::WORLD_MAX.y), 
		ShootEmUp::WORLD_MAX.z
		);
	mSceneNode->setPosition( initialPosition );
	
	if ( mEntity->isAttached() )
		mEntity->getParentSceneNode()->detachObject( mEntity );
	mSceneNode->attachObject( mEntity );
}
